39

Explore Your Deductive Logic—Sudoku

39

STEP 1 continued

For i =​ 1 To 9

For j =​ 1 To 9

    For k =​ 1 To 9

      cantbelist(i, j, k) =​ 0

    Next k

Next j

Next I

For i =​ 1 To 9

For j =​ 1 To 9

:

: {Compare each cell sbox(I,j) with every column, building the cantbelist (Step 2)}

:

: {Compare each cell sbox(I,j) with every row, building the cantbelist (Step 3)}

:

: {Compare each cell sbox(I,j) with every 3 by 3 grid, building the cantbelist (Step 4)}

:

Next j

Next i

STEP 2

Check all columns.

The reason we need two variables for column is because to build up the cantbelist

for each cell in the nine columns, you have to look at all the nine columns, hence

need to repeat this 9 × 9 =​ 81 times. The function NextEmptyLocation simply finds

the next empty value of the nine possible values for a cell in cantbelist. Note also

the use of the variable “foundincantbelist”. Initially set to 0, it gets set to 1 as soon

as a match is found. This helps with avoiding redundant information by checking

that the value of “foundincantbelist” is 1 before adding the number to cantbelist.

For k =​ 1 To 9

    If j <> k Then

      If sbox(i, k) > 0 Then

        foundincantbelist =​ 0

        For kk =​ 1 To 9

          If sbox(i, k) =​ cantbelist(i, j, kk) Then foundincantbelist =​ 1

        Next kk

        If foundincantbelist =​ 0 Then

          cantbelist(i, j, NextEmptyLocation(i, j)) =​ sbox(i, k)

        End If

      End IF

    End If

Next k

:

:

Function NextEmptyLocation(ByVal i, j)

For n =​ 1 To 9

If cantbelist(i, j, n) =​ 0 Then

NextEmptyLocation =​ n

n =​ 9

End If

Next n

End Function